package com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.childfragment;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.qingyiapp.R;
import com.example.qingyiapp.service.entify.FindChoicenessOneBean;
import com.example.qingyiapp.service.entify.FindChoicenessTwoBean;
import com.example.qingyiapp.service.entify.FindSynamicBean;
import com.example.qingyiapp.service.entify.FindTJBean;
import com.example.qingyiapp.service.entify.FindWaitYouBean;
import com.example.qingyiapp.service.presenter.PersenterMannaer;
import com.example.qingyiapp.service.view.IFindView;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.Address_data_Activity;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.adapter.FindRecyclerAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Creadted by Met·Wang on 2019/9/16 14:36
 * Describe: NullPointerException Write
 */
public class Recommend extends Fragment implements IFindView {

    ArrayList<FindTJBean.DataBean> list;
    FindRecyclerAdapter adapter;
    @BindView(R.id.find_recommend_xrecycler)
    XRecyclerView findRecommendXrecycler;
    Unbinder unbinder;

    PersenterMannaer iRequestPersenter;
    ImageView headImg;
    TextView headtv;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View inflate = inflater.inflate(R.layout.fragment_find_recommend, container, false);
        unbinder = ButterKnife.bind(this, inflate);

        list = new ArrayList<>();
        adapter = new FindRecyclerAdapter(list, getContext());
        findRecommendXrecycler.setAdapter(adapter);
        View inflate1 = LayoutInflater.from(getContext()).inflate(R.layout.fragment_find_recommend_head, container, false);
        headImg= inflate1.findViewById(R.id.find_recommend_head_image);
        headtv= inflate1.findViewById(R.id.find_recommend_head_text);
        findRecommendXrecycler.addHeaderView(inflate1);
        LinearLayoutManager manager=new LinearLayoutManager(getContext());
        findRecommendXrecycler.setLayoutManager(manager);
        iRequestPersenter=new PersenterMannaer(getContext(),"http://www.iqingyi.com/");

        iRequestPersenter.onCreate();
        iRequestPersenter.attchview2(this);
        iRequestPersenter.getFindRecommendJson();

        findRecommendXrecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override 
            public void onLoadMore() {

            }
        });


        adapter.setItemOnClick(new FindRecyclerAdapter.ItemOnClick() {
            @Override
            public void onClick(int i) {
                String pid = list.get(i).getPid();
                Intent intent = new Intent(getActivity(), Address_data_Activity.class);
                intent.putExtra("pid",pid);
                startActivity(intent);
            }
        });


        return inflate;

    }





    @Override
    public void getjsonstring(FindTJBean findTJBean) {


        Log.d("###", "getjsonstring: 进来了");
        List<FindTJBean.DataBean> data = findTJBean.getData();

        for (int i = 0; i < data.size(); i++) {
            if(i==0){
                headtv.setText(data.get(0).getHybrid_title());
                String hybrid_cover = data.get(0).getHybrid_cover();
                String[] split = hybrid_cover.split("!");
                Glide.with(getContext()).load(split[0]).into(headImg);
            }
            list.add(data.get(i));
        }


        adapter.notifyDataSetChanged();



    }

    @Override
    public void getChoicenessOneBean(FindChoicenessOneBean bean) {

    }

    @Override
    public void getChoicenessTwoBean(FindChoicenessTwoBean bean) {

    }

    @Override
    public void getFindWaitYou(FindWaitYouBean bean) {

    }

    @Override
    public void getFindSynamic(FindSynamicBean bean) {

    }

    @Override
    public void onDestroyView() {

        super.onDestroyView();
        unbinder.unbind();

    }
}
